<template>
  <div id="new-question">
    <group class="question-content">
      <x-textarea :placeholder="'问题标题'" v-model="titleval"  :show-counter="false" :rows="1" autosize></x-textarea>
      <x-textarea :placeholder="'问题内容'" v-model="contentval" :show-counter="false" :height="200" :rows="8" :cols="30"></x-textarea>
    </group>
    <upload-card :type="1"><p>仅支持：JPEG、GIF、PNG</p></upload-card>
    <div class="credit-btn">
      <flexbox>
        <flexbox-item>
          <x-button type="primary" @click.native="back">返回问题列表</x-button>
        </flexbox-item>
        <flexbox-item>
          <x-button type="warn" @click.native="apply">提交</x-button>
        </flexbox-item>
      </flexbox>
    </div>
  </div>
</template>
<script>
var timeStamp = String(Number(new Date()))
import { XTextarea, Group, Flexbox, FlexboxItem, XButton } from 'vux'
import api from '../../api'
import utils from '../../utils'
import UploadCard from '../../comment/UploadCard'
export default {
  data () {
    return {
      titleval: '',
      contentval: '',
      tokenval: ''
    }
  },
  components: {
    XTextarea,
    Group,
    Flexbox,
    FlexboxItem,
    XButton,
    UploadCard
  },
  created () {
    window.document.title = '新问题'
    this.tokenval = utils.AuthToken(timeStamp, this.$store.state.userkey)
    this.$store.commit('changeIndexConf', {
      uploadimg: ''
    })
    utils.jssdkwx(api, this.$wechat, this.$store)
  },
  methods: {
    back () {
      this.$router.push('/center/consult')
    },
    apply () {
      if (this.titleval === '') {
        this.$vux.toast.show({
          text: '请输入标题',
          type: 'warn',
          width: '4rem'
        })
      } else if (this.contentval === '') {
        this.$vux.toast.show({
          text: '请输入内容',
          type: 'warn',
          width: '4rem'
        })
      } else {
        api.newquestion({
          token: this.tokenval,
          timestamp: timeStamp,
          userId: this.$store.state.userID,
          title: this.titleval,
          content: this.contentval,
          picUrl: this.$store.state.uploadimg
        }).then(res => {
          console.log(res)
          if (res.status === 200) {
            this.$router.push('/center/consult')
          } else {
            this.$vux.toast.show({
              text: res.message,
              type: 'warn',
              width: '4rem'
            })
          }
        })
      }
    }
  }
}
</script>
